package flc.ast.fragment;

import android.Manifest;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.CameraActivity;
import flc.ast.adapter.AnalyseAdapter;
import flc.ast.adapter.RecordAdapter;
import flc.ast.adapter.ShotRecordAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.Content;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private StkResBean guideBean;
    private StkResBean skillBean;
    private AnalyseAdapter analyseAdapter;
    private List<String> listShow = new ArrayList<>();
    private ShotRecordAdapter recordAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.tvCamera.setOnClickListener(this);
        mDataBinding.rlPhotoGuide.setOnClickListener(this);
        mDataBinding.rlPhotoSkill.setOnClickListener(this);

        mDataBinding.rvAnalyseList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        analyseAdapter = new AnalyseAdapter();
        mDataBinding.rvAnalyseList.setAdapter(analyseAdapter);
        analyseAdapter.setOnItemClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new GridLayoutManager(mContext, 3));
        recordAdapter = new ShotRecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvCamera:
                getPermission();
                break;
            case R.id.rlPhotoGuide://拍照指南
                BaseWebviewActivity.open(mContext, guideBean.getUrl(), guideBean.getName());
                break;
            case R.id.rlPhotoSkill://构图技巧
                BaseWebviewActivity.open(mContext, skillBean.getUrl(), skillBean.getName());
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getGuideData();
        getSkillData();
        getAnalyseData();
    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getPermission() {
        StkPermissionHelper.permission(Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO).reqPermissionDesc(getString(R.string.get_camera_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                CameraActivity.sIsID = false;
                startActivity(CameraActivity.class);
            }
        }).request();
    }

    private void getGuideData() {
        StkResApi.getTagResourceList(this, AppConfig.GUIDE_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        guideBean = result.get(0);
                    }
                }
            }
        });
    }

    private void getSkillData() {
        StkResApi.getTagResourceList(this, AppConfig.SKILL_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        skillBean = result.get(0);
                    }
                }
            }
        });
    }

    private void getAnalyseData() {
        StkResApi.getTagResourceList(this, AppConfig.ANALYSE_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        analyseAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getData() {
        listShow.clear();
        List<File> files = FileUtils.listFilesInDir(PathUtils.getFilesPathExternalFirst() + Content.myShotWork);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
        }
    }

}